package com.palpiteiro.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.palpiteiro.bean.IBean;
import com.palpiteiro.bean.Meus_Papeis;

public class Meus_PapeisDAO extends IDAO {

	public ResultSet selectByIdUsuario(IBean bean) throws SQLException {
		Meus_Papeis meusPapeis = (Meus_Papeis) bean;
		
		String sql = "";
		PreparedStatement stmt;
		sql = "select id_meus_papeis,id_empresa,id_usuario,quantidade,vl_pago,dt_compra" +
				" from \"Usuario\".\"Meus_Papeis\"" +
				" where id_usuario = ?;";

		stmt = getConn().prepareStatement(sql);
		stmt.setInt(1, meusPapeis.getId_usuario());

		ResultSet rs = stmt.executeQuery();

		return rs;
	}
	
	@Override
	public ResultSet select(IBean bean) throws SQLException {
		Meus_Papeis meusPapeis = (Meus_Papeis) bean;
		
		String sql = "";
		PreparedStatement stmt;
		if(meusPapeis.getId_meus_papeis() != Integer.MIN_VALUE)
		{
			sql = "select id_meus_papeis,id_empresa,id_usuario,quantidade,vl_pago,dt_compra" +
					" from \"Usuario\".\"Meus_Papeis\"" +
					" where id_meus_papeis = ?;";

			stmt = getConn().prepareStatement(sql);
			stmt.setInt(1, meusPapeis.getId_meus_papeis());
		}else{
			sql = "select id_meus_papeis,id_empresa,id_usuario,quantidade,vl_pago,dt_compra" +
					" from \"Usuario\".\"Meus_Papeis\"" +
					" where id_empresa = ? and id_usuario = ?;";

			stmt = getConn().prepareStatement(sql);
			stmt.setInt(1, meusPapeis.getId_empresa());
			stmt.setInt(2, meusPapeis.getId_usuario());
		}
		ResultSet rs = stmt.executeQuery();
		
		return rs;
	}

	@Override
	public void insert(IBean bean) throws SQLException {
		Meus_Papeis meusPapeis = (Meus_Papeis) bean;
		
		String sql = "";
		sql = "insert into \"Usuario\".\"Meus_Papeis\"" +
				"(id_empresa,id_usuario,quantidade,vl_pago,dt_compra)" +
				" values " +
				" (?, ?, ?, ?, ?)";
		
		PreparedStatement stmt = getConn().prepareStatement(sql);
		stmt.setInt(1,meusPapeis.getId_empresa());
		stmt.setInt(2,meusPapeis.getId_usuario());
		stmt.setInt(3,meusPapeis.getQuantidade());
		stmt.setDouble(4,meusPapeis.getVl_pago());
		stmt.setDate(5, meusPapeis.getDt_compra());
		
		stmt.executeUpdate();
		
		stmt.close();
	}

	@Override
	public void update(IBean bean) throws SQLException {
		Meus_Papeis meusPapeis = (Meus_Papeis) bean;
		
		String sql = "";
		sql = "update \"Usuario\".\"Meus_Papeis\" " +
				"set id_empresa=?,id_usuario=?,quantidade=?,vl_pago=?,dt_compra=?" +
				" where id_meus_papeis=?";
		PreparedStatement stmt = getConn().prepareStatement(sql);
		stmt.setInt(1,meusPapeis.getId_empresa());
		stmt.setInt(2,meusPapeis.getId_usuario());
		stmt.setInt(3,meusPapeis.getQuantidade());
		stmt.setDouble(4,meusPapeis.getVl_pago());
		stmt.setDate(5, meusPapeis.getDt_compra());
		stmt.setInt(6,meusPapeis.getId_meus_papeis());

		stmt.executeUpdate();
		
		stmt.close();
	}

	public void delete(IBean bean) throws SQLException {
		Meus_Papeis meusPapeis = (Meus_Papeis) bean;
		
		if(meusPapeis.getQuantidade() != 0) return;
		
		String sql = "";
		sql = "delete from \"Usuario\".\"Meus_Papeis\" where id_meus_papeis = ?";
		
		PreparedStatement stmt = getConn().prepareStatement(sql);
		stmt.setInt(1,meusPapeis.getId_meus_papeis());
		
		stmt.executeUpdate();
		
		stmt.close();
	}
}
